package controllers
{
	import mx.core.Application;
	
	public class TimelineController extends Object
	{
		public function TimelineController()
		{
			super();
		}
		
		public function generateTimelineRange():void{
			// We display 200 frames in the timeline, ranging from globalTimeLinePosition - 100 to globalTimeLinePosition + 100
			var min:int = Application.application.timeline.timelineMin;
			var max:int = Application.application.timeline.timelineMax;
			var totalStates:Number = Application.application.timeline.totalStages;
			var percentage:Number = Number(Application.application.timeline.globalTimelinePosition) / 100.00;
			var scaleFactor:Number = Application.application.timeline.scaleFactor;
			
			min = totalStates * percentage - int(100 / scaleFactor);
			max = totalStates * percentage + int(100 / scaleFactor);
			
			if(min < 0)
				min = 0;
			
			if(max > Application.application.timeline.totalStages)
				max = Application.application.timeline.totalStages;
			
			Application.application.timeline.timelineMin = min;
			Application.application.timeline.timelineMax = max;
			
			// Reset timeline ticks
			Application.application.timeline.timelineTicks = new Array();
			var tickInterval:int = (max - min) / 20;
			for(var i:uint = 0;i < 20; i++){
				Application.application.timeline.timelineTicks.push(int(min + tickInterval * i));
			}
			
			if(!(max in Application.application.timeline.timelineTicks))
				Application.application.timeline.timelineTicks.push(max);
		}
		
		public function zoomIn():void{
			if(Application.application.timeline.scaleFactor == 8)
				return;
			
			Application.application.timeline.scaleFactor *= 2;
			generateTimelineRange();
		}

		public function zoomOut():void{
			if(Application.application.timeline.scaleFactor == 1)
				return;
				
			Application.application.timeline.scaleFactor /= 2;
			generateTimelineRange();
		}
		
		// Adjust the timeline scale according to the given state
		public function adjustTimeline(stateID:Number):void{
			var globalPosition:int = int(Number(stateID / Application.application.timeline.totalStages) * 100);
			Application.application.timeline.globalTimelinePosition = globalPosition;
			
			generateTimelineRange();
		}
		
	}
}